package com.luoye.unionlotto;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class WinningAnalyzer {
    private String dataDir="";

    private UnionlottoFromFileGetter unionlottoFromFileGetter;
    private UnionlottoFromNetGetter unionlottoFromNetGetter;
    private BuysUnionlottoFileReaderWriter buysUnionlottoFileReaderWriter;

    public WinningAnalyzer(String dataDir,String url) {
        this.dataDir = dataDir;
        this.unionlottoFromFileGetter=new UnionlottoFromFileGetter(dataDir);
        this.unionlottoFromNetGetter=new UnionlottoFromNetGetter(url);
        this.buysUnionlottoFileReaderWriter=new BuysUnionlottoFileReaderWriter(dataDir);
    }

    void printUnionlotto(Unionlotto unionlotto){
        //输出结果
        //System.out.print(unionlotto.getDate()+":");
        for(String redBall:unionlotto.getRedBalls()){
            System.out.print(redBall+" ");
        }
        System.out.println("- "+unionlotto.getBlueBall());
    }

    //
    public void analyze(){
        //从文件获取
        List<Unionlotto> rightUnionlottoList=this.unionlottoFromFileGetter.get();
        Unionlotto latest=null;
        if(rightUnionlottoList.size()>0){
            latest=rightUnionlottoList.get(0);
        }
        //从网络获取
        List<Unionlotto> unionlottoListTemp=this.unionlottoFromNetGetter.get(latest);
        if(latest==null){
            rightUnionlottoList=unionlottoListTemp;
        }else {
            List<Unionlotto> unionlottoListBase = new ArrayList<Unionlotto>();
            for (Unionlotto unionlotto : unionlottoListTemp) {
                if (unionlotto.getDate().equals(latest.getDate())) {
                    break;
                }
                unionlottoListBase.add(unionlotto);
            }
            unionlottoListBase.addAll(rightUnionlottoList);
            rightUnionlottoList = unionlottoListBase;
        }
        if(rightUnionlottoList.size()>0) {
            Unionlotto latestRight = rightUnionlottoList.get(0);
            //从文件获取上次购买的
            File file = new File(this.dataDir + "/buys");
            if (file.exists() && file.isDirectory()) {//是个目录
                File[] files = file.listFiles();
                List<String> dates = new ArrayList<String>();
                for (File file1 : files) {
                    String date = file1.getName().substring(0, file1.getName().indexOf(".txt"));
                    dates.add(date);
                }
                dates.sort(new Comparator<String>() {
                    public int compare(String o1, String o2) {
                        return o2.compareTo(o1);
                    }
                });
                //System.out.println(dates);
                if (dates.size() > 0) {
                    String date = dates.get(0);
                    List<Unionlotto> buys = this.buysUnionlottoFileReaderWriter.read(date);
                    if(latestRight.date.equals(date)){
                        LuckFunc luckFunc=new LuckFunc();
                        List<LuckEntity> luckEntityList = luckFunc.startlotto(buys, latestRight);
                        //打印中奖结果
                        System.out.println("\n\n"+date+":");
                        System.out.print("开奖号码:");
                        printUnionlotto(latestRight);
                        System.out.println("");
                        int i=0;
                        for(LuckEntity luckEntity:luckEntityList) {
                            System.out.print("购买号码:");
                            printUnionlotto(buys.get(i));
                            System.out.print("中奖号码:");
                            printUnionlotto(luckEntity.unionlotto);
                            System.out.println("中奖结果：" + luckEntity.result+"\n");
                            i++;
                        }
                    }else{
                        System.out.println("还没有开奖");
                    }
                }
            }
        }else{
            System.out.println("没有取到开奖结果");
        }
    }

    public static void main(String[] args){
        WinningAnalyzer winningAnalyzer=new WinningAnalyzer("D:\\\\download\\\\unionlotto","http://kaijiang.zhcw.com/zhcw/inc/ssq/ssq_wqhg.jsp");
        winningAnalyzer.analyze();
    }
}
